package com.gin.kotlin.no_gay_alliance.database.entity

import androidx.room.Entity
import androidx.room.PrimaryKey
import com.gin.nga.enums.status.TopicStatus
import com.gin.nga.response.reply.ReplyListBody
import java.time.ZonedDateTime

/**
 * 主题浏览历史
 * @property topicId 主题ID
 * @property timestamp 上次浏览时间
 * @property page 上次浏览页数
 * @property title 主题标题
 * @property authorId 作者ID，如果为负数则为匿名
 * @property authorName 作者用户名，可能为匿名格式
 * @property lastPosterName 最后一个回复者用户名
 * @property topicParent 所属合集名或版面名
 * @property replies 回复数
 * @property totalPage 总页数
 * @property color 标题颜色
 * @property bold 加粗
 * @property lineThrough 删除线
 * @property italic 斜体
 * @property postDatetime 发布时间
 * @constructor
 */
@Entity
data class TopicHistory(
    @PrimaryKey
    val topicId: Long,
    val timestamp: ZonedDateTime,
    val page: Int,
    val title: String,
    val totalPage: Int? = null,
    val authorId: String? = null,
    val authorName: String? = null,
    val lastPosterName: String? = null,
    val topicParent: String? = null,
    val replies: Int? = null,
    val titleColor: TopicStatus? = null,
    val bold: Boolean = false,
    val lineThrough: Boolean = false,
    val italic: Boolean = false,
    val postDatetime: ZonedDateTime?=null,
) {
    constructor(body: ReplyListBody) : this(
        topicId = body.topic!!.topicId!!,
        timestamp = ZonedDateTime.now(),
        page = body.page,
        title = body.topic!!.title!!,
        totalPage = body.totalPage,
        authorId = body.topic?.authorUid,
        authorName = body.topic?.authorName,
        lastPosterName = body.topic?.lastPosterName,
        topicParent = body.topic?.topicParent?.name ?: body.forum?.forumName,
        replies = body.topic?.replies,
        postDatetime = body.topic?.postDatetime,
        titleColor = body.topic?.getTopicStatus()?.firstOrNull { it.ordinal<=4 },
        bold = body.topic?.getTopicStatus()?.contains(TopicStatus.BOLD) ==true,
        lineThrough =body.topic?.getTopicStatus()?.contains(TopicStatus.LINE_THROUGH) ==true,
        italic = body.topic?.getTopicStatus()?.contains(TopicStatus.ITALIC) ==true,
    )
}